package com.lh.eurekaserver.Event;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class EurekaStateChangeListener {
    private final boolean listenSign = false;

    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
        if (listenSign)
            System.err.println(event.getServerId() + "\t" + event.getAppName() + " 服务下线");
    }

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        if (listenSign) {
            InstanceInfo instanceInfo = event.getInstanceInfo();
            System.err.println(instanceInfo.getAppName() + "进行注册");
        }
    }

    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        if (listenSign) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
            String myTime = simpleDateFormat.format(new Date());
            System.err.println(myTime + ":" + event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
        }
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        if (listenSign)
        System.err.println("注册中心启动");
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        if (listenSign)
            System.err.println("Eureka Server启动");
    }
}
